package com.ruoyi.ly.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ly.domain.Project;
import com.ruoyi.ly.domain.ServeExamineFlow;
import com.ruoyi.ly.domain.dto.PageServeUserDTO;
import com.ruoyi.ly.domain.vo.*;
import com.ruoyi.ly.mapper.*;
import com.ruoyi.ly.service.ServeExamineFlowService;
import com.ruoyi.ly.utils.PageResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ly.domain.ServeExaminePlan;
import com.ruoyi.ly.service.IServeExaminePlanService;

/**
 * 巡检计划Service业务层处理
 * 
 * @author wj
 * @date 2024-01-13
 */
@Service
public class ServeExaminePlanServiceImpl implements IServeExaminePlanService 
{
    @Autowired
    private ServeExaminePlanMapper serveExaminePlanMapper;

    @Autowired
    private ServeExamineFlowMapper serveExamineFlowMapper;

    @Autowired
    private ServeUserMapper serveUserMapper;

    @Autowired
    private SpaceBanMapper spaceBanMapper;

    @Autowired
    private SpaceMeetingMapper spaceMeetingMapper;

    @Autowired
    private WorkSpaceMapper workSpaceMapper;

    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询巡检计划
     * 
     * @param examinePlanId 巡检计划主键
     * @return 巡检计划
     */
    @Override
    public ServeExaminePlan selectServeExaminePlanByExaminePlanId(Long examinePlanId)
    {
        return serveExaminePlanMapper.selectServeExaminePlanByExaminePlanId(examinePlanId);
    }

    /**
     * 查询巡检计划列表
     * 
     * @param serveExaminePlan 巡检计划
     * @return 巡检计划
     */
    @Override
    public List<ServeExaminePlan> selectServeExaminePlanList(ServeExaminePlan serveExaminePlan)
    {
        return serveExaminePlanMapper.selectServeExaminePlanList(serveExaminePlan);
    }

    /**
     * 新增巡检计划
     * 
     * @param serveExaminePlan 巡检计划
     * @return 结果
     */
    @Override
    public int insertServeExaminePlan(ServeExaminePlan serveExaminePlan)
    {
        serveExaminePlan.setCreateTime(DateUtils.getNowDate());
        //serveExaminePlan.setCreateUid(SecurityUtils.getUserId().toString());//若以用户id创建，则需要获取当前用户id
        serveExaminePlan.setCreateUid("1");//没有登录的情况使用

        //要先把计划进行插入才能获得计划id 为后续插入巡检项目做准备
        int i = serveExaminePlanMapper.insertServeExaminePlan(serveExaminePlan);

        if(serveExaminePlan.getExamineProjectType().equals("1"))//区域
        {

            String chooseMeets = serveExaminePlan.getChooseMeets();//选择的功能场地

            List<String> collect1 = Arrays.stream(chooseMeets.split(",")).collect(Collectors.toList());

            collect1.forEach((item) ->{

                //去掉[]
                String id = StringUtils.strip(item,"[]");

                //将计划插入到巡检记录里面
                ServeExamineFlow serveExamineFlow = new ServeExamineFlow();
                serveExamineFlow.setExaminePlanId(serveExaminePlan.getExaminePlanId());
                serveExamineFlow.setType(serveExaminePlan.getExamineProjectType());
                serveExamineFlow.setKeyId(0L);// keyId为0,表示是计划,而不是巡检记录
                serveExamineFlow.setTenantTel("1111");//TODO 默认插入固定值
                //根据功能场地id查询功能场地名称
                String name = spaceMeetingMapper.getName(Long.valueOf(id));
                serveExamineFlow.setTenantName(name);//TODO 不确定
                //插入项目的地址 经纬度
                ProjectUpdateVO projectUpdateVO = projectMapper.selectById(Long.valueOf(serveExaminePlan.getProjectId()));
                serveExamineFlow.setExamineFlowAddress(projectUpdateVO.getProjectAddress());
                serveExamineFlow.setExamineFlowGpsLat(projectUpdateVO.getProjectGpsx());
                serveExamineFlow.setExamineFlowGpsLon(projectUpdateVO.getProjectGpsy());

                serveExamineFlowMapper.insert(serveExamineFlow);
            });

            String chooseSiteGroups = serveExaminePlan.getChooseSiteGroups();//选择的工作区
            List<String> collect2 = Arrays.stream(chooseSiteGroups.split(",")).collect(Collectors.toList());
            collect2.forEach((item) ->{

                //去掉[]
                String id = StringUtils.strip(item,"[]");

                //将计划插入到巡检记录里面
                ServeExamineFlow serveExamineFlow = new ServeExamineFlow();
                serveExamineFlow.setExaminePlanId(serveExaminePlan.getExaminePlanId());
                serveExamineFlow.setType(serveExaminePlan.getExamineProjectType());
                serveExamineFlow.setKeyId(0L);// keyId为0,表示是计划,而不是巡检记录
                serveExamineFlow.setTenantTel("1111");//TODO 默认插入固定值
                //根据工作区id查询会议室名称

                WorkSpaceDetailVO workSpaceDetailVO = workSpaceMapper.selectById(Long.valueOf(id));
                serveExamineFlow.setTenantName(workSpaceDetailVO.getSiteGroupName());//TODO 不确定
                //插入项目的地址 经纬度
                ProjectUpdateVO projectUpdateVO = projectMapper.selectById(Long.valueOf(serveExaminePlan.getProjectId()));
                serveExamineFlow.setExamineFlowAddress(projectUpdateVO.getProjectAddress());
                serveExamineFlow.setExamineFlowGpsLat(projectUpdateVO.getProjectGpsx());
                serveExamineFlow.setExamineFlowGpsLon(projectUpdateVO.getProjectGpsy());
                serveExamineFlowMapper.insert(serveExamineFlow);
            });

        }else if(serveExaminePlan.getExamineCateType().equals("2"))//楼栋
        {
            String chooseBans = serveExaminePlan.getChooseMeets();//选择的楼栋

            List<String> collect = Arrays.stream(chooseBans.split(",")).collect(Collectors.toList());
            collect.forEach((item) ->{

                //去掉[]
                String id = StringUtils.strip(item,"[]");

//                //将计划插入到巡检记录里面
                ServeExamineFlow serveExamineFlow = new ServeExamineFlow();
                serveExamineFlow.setExaminePlanId(serveExaminePlan.getExaminePlanId());
                serveExamineFlow.setType(serveExaminePlan.getExamineProjectType());
                serveExamineFlow.setKeyId(0L);// keyId为0,表示是计划,而不是巡检记录
                serveExamineFlow.setTenantTel("1111");//TODO 默认插入固定值


                //通过楼栋id查询楼栋名称
                String tenantName = spaceBanMapper.getName(Long.valueOf(id));
                serveExamineFlow.setTenantName(tenantName);//TODO 不确定
                //插入项目的地址 经纬度
                ProjectUpdateVO projectUpdateVO = projectMapper.selectById(Long.valueOf(serveExaminePlan.getProjectId()));
                serveExamineFlow.setExamineFlowAddress(projectUpdateVO.getProjectAddress());
                serveExamineFlow.setExamineFlowGpsLat(projectUpdateVO.getProjectGpsx());
                serveExamineFlow.setExamineFlowGpsLon(projectUpdateVO.getProjectGpsy());

                serveExamineFlowMapper.insert(serveExamineFlow);
            });
        }

        return i;
    }




    /**
     * 修改巡检计划
     * 
     * @param serveExaminePlan 巡检计划
     * @return 结果
     */
    @Override
    public int updateServeExaminePlan(ServeExaminePlan serveExaminePlan)
    {
        serveExaminePlan.setUpdateTime(DateUtils.getNowDate());
        return serveExaminePlanMapper.updateServeExaminePlan(serveExaminePlan);
    }

    /**
     * 批量删除巡检计划
     * 
     * @param examinePlanIds 需要删除的巡检计划主键
     * @return 结果
     */
    @Override
    public int deleteServeExaminePlanByExaminePlanIds(Long[] examinePlanIds)
    {
        return serveExaminePlanMapper.deleteServeExaminePlanByExaminePlanIds(examinePlanIds);
    }

    /**
     * 删除巡检计划信息
     * 
     * @param examinePlanId 巡检计划主键
     * @return 结果
     */
    @Override
    public int deleteServeExaminePlanByExaminePlanId(Long examinePlanId)
    {
        return serveExaminePlanMapper.deleteServeExaminePlanByExaminePlanId(examinePlanId);
    }


    //分页查询管理员
    @Override
    public PageResult pageQuery(PageServeUserDTO pageServeUserDTO) {
        PageHelper.startPage(pageServeUserDTO.getPage(), pageServeUserDTO.getPageSize());
        Page<PageServeUserVO> page = serveUserMapper.pageQuery(pageServeUserDTO);
        long total = page.getTotal();
        List<PageServeUserVO> records = page.getResult();
        return new PageResult(total,records);
    }

    //根据openId显示对应的巡检信息(小程序端)
    @Override
    public List<ServePlanAPPVO> selectByOpenId(@Param("openId") String openId, @Param("examineCateType")String examineCateType) {

        //取出所有的计划
        List<ServeExaminePlan> serveExaminePlanList = serveExaminePlanMapper.selectAll();
        List<ServePlanAPPVO> servePlanAPPVOList = new ArrayList<>();
        serveExaminePlanList.forEach(serveExaminePlan ->{
            if(serveExaminePlan.getExamineAllowUserIds().contains(openId) && serveExaminePlan.getExamineCateType().equals(examineCateType))
            {
                ServePlanAPPVO servePlanAPPVO = new ServePlanAPPVO();
                BeanUtils.copyProperties(serveExaminePlan,servePlanAPPVO);
                servePlanAPPVOList.add(servePlanAPPVO);
            }
        });
        return servePlanAPPVOList;
    }


    //根据巡检计划id显示对应的巡检计划信息(小程序端)   keyId 用于区分待巡检和已巡检
    @Override
    public List<ServeFlowAPPVO> selectByPlanId(@Param("examinePlanId")Long examinePlanId, @Param("keyId")Long keyId) {
        List<ServeFlowAPPVO> serveFlowAPPVOList = serveExaminePlanMapper.selectByPlanId(examinePlanId,keyId);
        return serveFlowAPPVOList;
    }


    //修改对应巡检人
    @Override
    public void update(@Param("name")String name,@Param("examinePlanId")Long examinePlanId){
        String openId = serveUserMapper.selectByName(name);
        //修改对应计划的openId
        serveExaminePlanMapper.update(openId,examinePlanId);
    }
}
